
$(function() {
	Tip.emp('负责人');
	
	$('#category_s').selectChain({
		target: $('#type_s'),
		key: "code",
		value: "name",
		url: 'document.php?do=Document.ajax_category',
		data: { ajax: true }
	}).trigger('change');
});

var Document = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.name = form.name.value;
				body.category = form.category.value;
				body.type = form.type.value;

				body.secrecy = form.secrecy.value;
				body.expired = form.expired.value;

				body.content = form.content.value;

				$(this).dialog("close");

				$.post('document.php?do=Document.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	clean_cache: function () {
		if (!confirm("确认是否清除缓存？")) {
			return false;
		}

		$.get('document.php?do=Document.ajax_category_type', function(data) {
			alert("操作成功！");
		});
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'document.php?do=Document.edit&id=' + id + p2;
		var c2 = 'document.php?do=Document.view&id=' + id + p2;
		
		var c3 = 'document.php?do=DocumentInfo.findPost&document_s=' + id;	
		var c4 = 'document.php?do=DocumentInfo.add&id=' + id;
		
		var a1 = ['编辑信息', 'url', 'Document.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];

		var a3 = ['相关信息', 'url', 'list'];
		var a4 = ['上传文档', 'url', 'Document.dialog_info', '650x430'];
		
		var s = c1 + ", " + c2 + ", " + c3 + ", " + c4;

		return this.popup_(e, [a1, a2, a3, a4], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'document.php?do=Document.ss&id=' + id;
		var bb = ['修改状态', 'url', 'dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		$.getJSON('document.php?do=Document.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {

					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'document.php?do=Document.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$('#result').html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	}, 

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		$.getJSON('document.php?do=Document.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {

					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.name = form.name.value;
						body.category = form.category.value;
						body.type = form.type.value;

						body.secrecy = form.secrecy.value;
						body.expired = form.expired.value;

						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'document.php?do=Document.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$('#result').html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_info: function (url, title, xy) {
		$.getJSON('document.php?do=Document.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {

					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"上传": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = { };
						body.id = form.id.value;
						body.content = form.content.value;

						var url = 'document.php?do=DocumentInfo.addPost';

						$.post(url, body, function(data) {
							var a = $.evalJSON(data);

							var s = "document.php?do=DocumentInfo.ajax_upload&id=" + a.id + "&name=" + $("#filename").val();
							$('#userfile').uploadifySettings('script', s);

							if ($("#_upload").val() == "1") {
								$("#dialog_add").dialog("close");
							} else {
								$('#userfile').uploadifyUpload();
							}

							Document.list_();
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	list_: function () {
		var url = 'document.php?do=Document.findPost';

		var p2 = $.cookie('p2');
		if (p2 != null) {
			url += p2;
		}

		$.get(url, function(data) {
			$('#result').html(data);
		});
	}
};
